package com.yvan.platform;

import java.math.BigDecimal;
import java.util.regex.Pattern;


/**
 * 处理数字格式
 * Created by zzy on 2018/1/3.
 */
public class NumberUtil {


    private static final Double MONEY_RANGE_UP = 0.01;

    private static final Double MONEY_RANGE_DOWN = -0.01;

    /**
     * 将数字转换成字符串，如果小数点后都为0，则取整
     *
     * @return
     */
    public static String decimalToString(Object number) {

        if (StringUtils.isNullOrEmpty(number)) {
            return null;
        }
        String result = String.valueOf(number);
        String numberStr = String.valueOf(number);
        if ("null".equals(number)) {
            return null;
        }
        if (isNumeric(numberStr)) {
            if (numberStr.indexOf(".") > 0) {
                //正则表达
                result = numberStr.replaceAll("0+?$", "");//去掉后面无用的零
                String lastStr = result.substring(result.length() - 1, result.length());
                if (".".equals(lastStr)) {
                    result = result.replaceAll("[.]$", "");//如最后一位是.则去掉
                }
            }
        }
        return result;
    }

    /**
     * 返回两位小数的传
     *
     * @param number
     * @return
     */
    public static String dc2srWith2(Object number) {
        if (number instanceof BigDecimal) {
            return String.valueOf(((BigDecimal) number).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        if (StringUtils.isNullOrEmpty(number)) {
            return null;
        }
        String result = String.valueOf(number);
        if (isNumeric(result)) {
            BigDecimal decimal = new BigDecimal(result);
            result = String.valueOf(decimal.setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        return result;
    }

    /**
     * 返回无小数的值
     *
     * @param number
     * @return
     */
    public static String dc2srWith0(Object number) {
        if (StringUtils.isNullOrEmpty(number)) {
            return null;
        }
        String result = String.valueOf(number);
        if (isNumeric(result)) {
            BigDecimal decimal = new BigDecimal(result);
            result = String.valueOf(decimal.setScale(0, BigDecimal.ROUND_HALF_UP));
        }
        return result;
    }

    /**
     * 返回两位数的double
     *
     * @param number
     * @return
     */
    public static BigDecimal dc2flWith2(Object number) {
        if (number == null) {
            return null;
        }
        String str = number.toString();
        if (StringUtils.isNullOrEmpty(str)) {
            return null;
        }
        BigDecimal result = (new BigDecimal(dc2srWith2(number))).setScale(2, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    /**
     * 判断字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]+)?$");
        boolean result = pattern.matcher(str).matches();
        return result;
    }

    /**
     * 比较大小,精度为0.01
     *
     * @param d1
     * @param d2
     * @return
     */
    public static Boolean equels(Double d1, Double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        int result = b1.compareTo(b2);
        if (result < MONEY_RANGE_UP && result > MONEY_RANGE_DOWN) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 返回精度为一位小数，多余直接舍掉
     *
     * @param number
     * @return
     */
    public static BigDecimal oneDigit(BigDecimal number) {
        return number.setScale(1, BigDecimal.ROUND_DOWN);
    }

    /**
     * 返回整数 全部舍掉
     *
     * @param number
     * @return
     */
    public static BigDecimal noneDigit(BigDecimal number) {
        return number.setScale(0, BigDecimal.ROUND_DOWN);
    }
}
